package cn.gok.homework;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;

public class Homework1 {
    private static void 大小数运算() {
        double a = 4444444.4444;
        BigDecimal b1 = new BigDecimal(a);
        double b = 3.333333;
        BigDecimal b2 = new BigDecimal(b);

        BigDecimal result = b1.divide(b2, 2, RoundingMode.HALF_UP); //保留2位，四舍五入
        System.out.println(result.toString());
    }

    private static int getDaySub(int year, int month, int date) {
        Calendar c = Calendar.getInstance();
        c.set(year, 0, 0, 0, 0, 0);
        long sTime = c.getTimeInMillis();
        c.set(year, month - 1, date, 0, 0, 0);
        c.add(Calendar.WEDNESDAY, -1);
        long dTime = c.getTimeInMillis();
        return (int) ((dTime - sTime) / (1000 * 60 * 60 * 24));
    }

    private static void recursiveOutputPath(File file) {
        File file1[] = file.listFiles();//遍历目录获取的子文件、子目录
        for (File file2 : file1) {
            if (file2.isFile()) {
                String[] t = file2.getName().split(".");
                if (file2.getName().endsWith(".java")) {
                    System.out.println(".java文件绝对路径：" + file2.getAbsolutePath());
                }

            } else if (file2.isDirectory()) {
                recursiveOutputPath(file2);
            }
        }
    }

    public static void createFile(File file) throws IOException {
        File fileParent = file.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdir();
            fileParent.mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
    }

    public static  void  copyFile(File f1,File f2){
        try {
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);

            FileWriter fw = new FileWriter(f2);
            PrintWriter pw = new PrintWriter(fw);

            String temp = "";
            while ((temp = br.readLine()) != null) {
                pw.println(temp);
                pw.flush();
            }
            System.out.println("传输完成");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void transCoding(File file1,File file2) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file1),"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file2),"GBK");
        int len = 0;
        while ((len = isr.read()) != -1){
            //4.使用OutputTreamWrite对象中的方法write，把读取的数据写入到文件中
            osw.write(len);
        }
        //5.释放资源
        osw.close();
        isr.close();

    }
    public static void main(String[] args) {
//        1
//        大小数运算();
//        2
//        System.out.println(getDaySub(2001,2,1));
//        3
//        File file = new File("I:\\workspace_idea\\javase_project\\day4\\src");
//        recursiveOutputPath(file);
//        4
        File file1 = new File("D:\\HelloWorld.txt");
        try {
            createFile(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("是否为目录" + file1.isDirectory());
        File file2 = new File("D:\\IOTest\\a.txt");
        try {
            createFile(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        copyFile(file1,file2);
//        5
        try {
            transCoding(file1,file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
